package com.droidblackbox.history;

import java.util.Hashtable;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.droidblackbox.R;

public class HistoryAdaptor<T extends Object> extends
		ArrayAdapter<FileListInfo> {
	private LayoutInflater inflater = null;

	public Hashtable<Integer, View> hashConvertView = new Hashtable<Integer, View>();

	private List<FileListInfo> list = null;
	private int resource;

	public HistoryAdaptor(Context context, int resource,
			List<FileListInfo> objects) {
		super(context, resource, objects);
		this.resource = resource;
		list = objects;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (hashConvertView.containsKey(position) == false) {
			convertView = inflater.inflate(resource, parent, false);

			holder = new ViewHolder();

			holder.textView = (TextView) convertView
					.findViewById(R.id.fileName);
			holder.checkBox = (CheckBox) convertView
					.findViewById(R.id.fileCheckBox);

			holder.checkBox.setChecked(((ListView) parent)
					.isItemChecked(position));
			holder.checkBox.setFocusable(false);
			holder.checkBox.setClickable(false);

			convertView.setTag(holder);
			hashConvertView.put(position, convertView);
		} else {
			convertView = (View) hashConvertView.get(position);
			holder = extracted(convertView);
		}
		holder.textView.setText(list.get(position).getName());
		holder.checkBox.setChecked(list.get(position).isChecked());

		return convertView;
	}

	@SuppressWarnings("unchecked")
	private ViewHolder extracted(View convertView) {
		return (ViewHolder) convertView.getTag();
	}

	class ViewHolder {
		public TextView textView = null;
		public CheckBox checkBox = null;
	}

}
